package com.fs.groupSeven.controller;

import com.fs.groupSeven.pojo.Class;
import com.fs.groupSeven.pojo.College;
import com.fs.groupSeven.pojo.Teacher;
import com.fs.groupSeven.pojo.User;
import com.fs.groupSeven.service.SubjectService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fs.groupSeven.pojo.Subject;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author： mjx
 * @createDate: 2020/9/28
 * @version: 1.0
 */

//注明这是一个IOC容器管理的bean
@Controller
@RequestMapping("/subject")
public class SubjectManageController {
    @Autowired//依赖注入 自动装配subjectService
    private SubjectService subjectService;


    //注明 dispatcherServlet将分发/hello的请求到helloWord方法这儿来
    //ModelMap、Model都能0进行给jsp界面传递参数
    @RequestMapping("/subjectView")
    public String  subjectView(ModelMap modelMap,WebRequest request){
        String collegeId = request.getParameter("collegeId").trim();
        modelMap.addAttribute("collegeId",collegeId);
        return "subjectManageMjx/javaSubjectManage";
    }

    @RequestMapping("/showSubjects")
    public String  showSubjects(ModelMap modelMap,WebRequest request){
        String subjectId = request.getParameter("subjectId").trim();
        modelMap.addAttribute("subjectId",subjectId);
        System.out.println("跳转课程详情时的课程id："+subjectId);
        return "subjectManageMjx/showNews";
        /*使用视图解析器后，返回一个jsp的名字即可，前后缀已经由视图解析器帮我们完成*/
//        return "/WEB-INF/jsp/user.jsp";
    }
    @RequestMapping("/showTeachers")
    public String  showTeachers(ModelMap modelMap,WebRequest request){
        String teacherId = request.getParameter("teacherId").trim();
        modelMap.addAttribute("teacherId",teacherId);
        System.out.println("跳转老师详情时的老师id："+teacherId);
        return "subjectManageMjx/showTeacher";
        /*使用视图解析器后，返回一个jsp的名字即可，前后缀已经由视图解析器帮我们完成*/
//        return "/WEB-INF/jsp/user.jsp";
    }

 /*   @RequestMapping("/getSubjectList")
    @ResponseBody//告诉框架返回的格式为json字符串类型，放在响应体里面
    public  Map<String,Object>  getSubjectList(){

        List<Subject> subjectList = subjectService.getSubjectList();
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取用户数据成功");
        result.put("subjectList",subjectList);
        return result;
    }*/

    @RequestMapping("/getOneSubject")
    @ResponseBody
    public Map<String,Object> getOneSubject(Integer subjectId){
        Map<String,Object> result = new HashMap<>();
        Subject subject = subjectService.getOneBySubjectId(subjectId);
//        System.out.println("获取到院级id："+subject.getCollegeId());
        System.out.println("获取到subject："+subject);
        if(subject == null){
            result.put("code",-1);
            result.put("message","获取课程数据失败");
        }else{
            result.put("code",0);
            result.put("message","获取课程数据成功");
            result.put("subject",subject);
        }
        return result;
    }

    //修改课程信息
    @RequestMapping("/editSubject")
    @ResponseBody
    public Map<String,Object> editSubject(@RequestBody Subject subject){
//        System.out.println("编辑课程信息进入控制层subjectId"+subjectId);
        Map<String,Object> result = new HashMap<>();
        boolean modified;
        if(subject.getSubjectId()>0) {
            modified = subjectService.modifyOneSubject(subject);
        }else {
            modified = subjectService.addOneSubject(subject);
        }

        if(!modified){
            result.put("code",-1);
            result.put("message","编辑课程数据失败");
        }else{
            result.put("code",0);
            result.put("message","编辑课程数据成功");
            result.put("subject",subject);
        }
        return result;
    }

    @RequestMapping("/getListByPage")
    @ResponseBody//告诉框架返回的格式为json字符串类型，放在响应体里面
    public Map<String,Object> getListByPage(Integer currentPage,
                                            Integer pageSize,Integer collegeId){

        PageInfo<Subject> pageInfo = subjectService.getSubjectsByPage(currentPage, pageSize,collegeId);

        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取课程数据成功");
        result.put("subjectList",pageInfo.getList());//返回查询出来的分页数据
        result.put("sum",pageInfo.getTotal());
        result.put("totalPage",pageInfo.getPages());//返回当前总共有多少页
        return result;
    }

    @RequestMapping("/deleteOneBySubject")
    @ResponseBody
    public Map<String,Object> deleteOneBySubject(Subject subject){
        Map<String,Object> result = new HashMap<>();
        boolean deleted = subjectService.removeOneSubject(subject);
        if(!deleted){
            result.put("code",-1);
            result.put("message","删除失败");
        }else{
            result.put("code",0);
            result.put("message","删除成功");
        }
        return result;
    }

    //获得老师们
     @RequestMapping("/getTeachers")
    @ResponseBody//告诉框架返回的格式为json字符串类型，放在响应体里面
    public  Map<String,Object>  getTeacherList(Integer collegeId){
//         System.out.println("查询老师列表时的collegeId为"+collegeId);
        List<Teacher> teacherList = subjectService.getTeacherList(collegeId);
//         System.out.println("控制层的老师列表："+teacherList);
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取用户数据成功");
        result.put("teacherList",teacherList);
        return result;
    }
    @RequestMapping("/querySubjectByName")
    @ResponseBody
    public Map<String,Object> queryUserByAddress(String subjectName){
//        System.out.println(text);
        Map<String,Object> result = new HashMap<>();
  /*      List<Subject> subjectList = subjectService.getSubjectThatNameLike(subjectName);
        result.put("code",0);
        result.put("message","获取用户数据成功");
        result.put("subjectList",subjectList);*/
        result.put("subjectName",subjectName);
        return result;
    }

    @RequestMapping("/getListByPageAndSubjectName")
    @ResponseBody//告诉框架返回的格式为json字符串类型，放在响应体里面
    public Map<String,Object> getListByPageAndSubjectName(Integer currentPage,
                                            Integer pageSize,Integer collegeId,String subjectName){

        PageInfo<Subject> pageInfo = subjectService.getListByPageAndSubjectName(currentPage, pageSize,collegeId,subjectName);

        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取课程数据成功");
        result.put("sum",pageInfo.getTotal());
        result.put("subjectList",pageInfo.getList());//返回查询出来的分页数据

        result.put("totalPage",1);//返回当前总共有多少页
        return result;
    }

    @RequestMapping("/getCollegeList")
    @ResponseBody//告诉框架返回的格式为json字符串类型，放在响应体里面
    public  Map<String,Object>  getCollegeList(){

        List<College> collegeList = subjectService.getCollegeList();
//        System.out.println(collegeList);
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取用户数据成功");
        result.put("collegeList",collegeList);
        return result;
    }

    @RequestMapping("/getClassList")
    @ResponseBody//告诉框架返回的格式为json字符串类型，放在响应体里面
    public  Map<String,Object>  getClassList(Integer subjectId){

//        System.out.println("在获取班级列表时获得学科的id："+subjectId);
        List<Class> classList = subjectService.getClassList(subjectId);
        Map<String,Object> result = new HashMap<>();
        result.put("code",0);
        result.put("message","获取班级集合成功");
        result.put("classList",classList);
        return result;
    }

}
